<template>
  <van-icon
    :color="value === 1 ? '#e5645f' : ''"
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    @click="Like"
  />
</template>

<script>
import { addLike, deleteLike } from '@/api/article'
export default {
  name: 'LikeArticle',
  props: {
    value: {
      type: Number,
      required: true
    },
    acticleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {}
  },
  created () {},
  mounted () {},
  methods: {
    async Like () {
      let status = -1
      try {
        if (this.value === 1) {
          // 如果已经点赞就取消
          await deleteLike(this.acticleId)
        } else {
          // 没点赞就点赞
          await addLike(this.acticleId)
          status = 1
        }
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast('操作失败')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
